<?php
namespace app\controllers;

use app\common\corelib\CacheHelper;
use app\common\PayUtil;
use app\logic\CommonLogic;
use app\logic\UserLogic;
use app\models\Article;
use app\models\Chapter;
use app\models\Paper;
use Yii;

//前端个人中心接口
class IndexController extends BaseController
{

    const LIMIT = 10;
	public function actionIndex()
	{
	    /*$pu = new PayUtil();
	    $res = $pu->getUrl('2018072755100999','20180728132900',0.01,'test');
	    header("Location:$res");exit;*/
        $this->response(time());
	}

	public function actionCallback(){
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
        if($postObj->MsgType == "event" && $postObj->Event == "subscribe"){
            $userLogic = new UserLogic();
            $userLogic->addUser($postObj->FromUserName);
        }
    }

    public function actionTest1(){
	    jump("index/test2");
    }

    public function actionTest2(){
        echo "ok";exit;
    }
    //我的
    public function actionMy(){
        $open_id = $this->getQueryparams('open_id',"");
        $userLogic = new UserLogic();
        $user = $userLogic->findByOpenid($open_id);
        if($user['first_buy_time']){
            $time = time();
            $date=floor(($time-strtotime($user['first_buy_time']))/86400);
            $hour=floor(($time-strtotime($user['first_buy_time']))%86400/3600);
            $minute=floor(($time-strtotime($user['first_buy_time']))%86400%3600/60);
            $user['time'] = $date."天".$hour."小时".$minute."分钟";
        }else{
            $user['time'] = "";
        }
        if($user['study_time']){
            $hour=floor($user['study_time']/3600);
            $minute=floor($user['study_time']%86400%3600/60);
            $user['study_time'] = $hour.'小时'.$minute.'分';
        }else{
            $user['study_time'] = 0;
        }

        $this->response($user);
    }

    public function actionMyFile(){
        $open_id = $this->getQueryparams('open_id',"");
        $userLogic = new UserLogic();
        $user = $userLogic->findByOpenid($open_id);
        $user['in_time'] = date('Y-m-d',strtotime($user['in_time']));
        $this->response($user);
    }

    public function actionUpdateMyFile(){
        $open_id = $this->getQueryparams('open_id',"");
        $mobile = $this->getQueryparams('mobile',"");
        $email = $this->getQueryparams('email',"");
        $userLogic = new UserLogic();
        $userLogic->updateMyFile($open_id,$mobile,$email);
        $this->response();
    }
    //添加学习历史
    public function actionAddStudyHistory(){
        $open_id = $this->getQueryparams('open_id',"");
        $type = $this->getQueryparams('type',0);
        $id = $this->getQueryparams('id',0);
        $userLogic = new UserLogic();
        $history_id = $userLogic->addStudyHistory($open_id,$type,$id);
        $this->response(['history_id'=>$history_id]);
    }
    //更新学习历史
    public function actionUpdateStudyHistory(){
        $history_id = $this->getQueryparams('history_id',1);
        $userLogic = new UserLogic();
        $userLogic->updateStudyHistory($history_id);
        $this->response();
    }
    //获取学习历史
    public function actionStudyHistory(){
        $open_id = $this->getQueryparams('open_id',"");
        $userLogic = new UserLogic();
        $user = $userLogic->findByOpenid($open_id);
        $historys = $userLogic->studyHistory($open_id);
        $article_id = $chapter_id = $paper_id = [];
        foreach($historys AS $key=>&$value){
            $value['pic'] = \Yii::$app->params['upload']['upload_domain'].$value['pic'];
            if($value['type']==0){
                $article_id[] = $value['id'];
            }
            if($value['type']==1){
                $chapter_id[] = $value['id'];
            }
            if($value['type']==2){
                $paper_id[] = $value['id'];
            }
        }
        $in_article_id = $in_chapter_id = $in_paper_id = [];
        $article = Article::find()->where(['article_id'=>$article_id,'is_del'=>0])->asArray()->all();
        foreach($article AS $k1=>$v1){
            $in_article_id[] = $v1['article_id'];
        }
        $chapter = Chapter::find()->where(['chapter_id'=>$chapter_id,'is_del'=>0])->asArray()->all();
        foreach($chapter AS $k2=>$v2){
            $in_chapter_id[] = $v2['chapter_id'];
        }
        $paper = Paper::find()->where(['paper_id'=>$paper_id,'is_del'=>0])->asArray()->all();
        foreach($paper AS $k3=>$v3){
            $in_paper_id[] = $v3['paper_id'];
        }
        foreach($historys AS $kk=>$vv){
            if($vv['type']==0){
                if(!in_array($vv['id'],$in_article_id))
                    array_splice($historys, $kk, 1);
            }
            if($vv['type']==1){
                if(!in_array($vv['id'],$in_chapter_id))
                    array_splice($historys, $kk, 1);
            }
            if($vv['type']==2){
                if(!in_array($vv['id'],$in_paper_id))
                    array_splice($historys, $kk, 1);
            }
        }
        $this->response(['user'=>$user,'history'=>$historys]);
    }

    public function actionGetMessage(){
        $commonLogic = new CommonLogic();
        $res = $commonLogic->getMessage();
        $this->response($res);
    }

}
